package me.winter.imgop;

// Lossy Algorithm

public class WaveletSimplifier extends ImageSimplifier {

    public WaveletSimplifier() {

    }

    @Override
	public int [][] encodePixels( int [][] shades ) {
        int width  = shades.length;
        int height = shades[0].length;
        int [][] result = new int[width][height];

        for (int y = 0; y < height; ++y) {
            for (int x = 0; x < width / 2; ++x) {

                result[x][y] = (shades[2 * x][y] + shades[2 * x + 1][y] ) / 2;

                if (width % 2 == 0) {
                    result[width / 2 + x    ][y] = (shades[2 * x][y] - shades[2 * x + 1][y]) / 2;
                }
                else {                    
                    result[width / 2 + x + 1][y] = (shades[2 * x][y] - shades[2 * x + 1][y]) / 2;
                    result[width / 2        ][y] = shades[width - 1][y];
                }
            }
        }

        return result;
    }

    @Override
	public int [][] decodePixels (int [][] shades ) {
        int width  = shades.length;
        int height = shades[0].length;
        int [][] result = new int[width][height];

        for (int y = 0; y < height; ++y) {
            for (int x = 0; x < width / 2; ++x) {

                if (width % 2 == 0) {
                    result[2 * x    ][y] = shades[x][y] + shades[width / 2 + x][y];
                    result[2 * x + 1][y] = shades[x][y] - shades[width / 2 + x][y]; 
                } else {
                    result[2 * x    ][y] = shades[x][y] + shades[width / 2 + x + 1][y];
                    result[2 * x + 1][y] = shades[x][y] - shades[width / 2 + x + 1][y];
                    result[width - 1][y] = shades[width / 2][y];
                }                
            }
        }

        return result;
    }
}